package com.shuoyue.ycgk.ui.papergroups.hotquestion;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Paper;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.papergroups.PaperContract;
import com.shuoyue.ycgk.ui.papergroups.adapter.PaperSetAdapter;
import com.shuoyue.ycgk.ui.papergroups.groupcommon.PaperCommonInfoActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HotQuestionActivity extends BaseMvpActivity<PaperContract.Presenter>
        implements PaperContract.View {
    public static void start(Context sourse, Type type) {
        sourse.startActivity(new Intent(sourse, HotQuestionActivity.class)
                .putExtra("type", type));
    }

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_sigle_list;
    }


    protected Type type;
    PaperSetAdapter paperSetAdapter;


    @Override
    protected void initView() {
        pageTitle.setText("时政热点");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getPaperSet(type.getId(), null, 2
                    , false);
            refreshLayout.finishRefresh(2000);
        });

        paperSetAdapter = new PaperSetAdapter(null);
        recyclerView.setAdapter(paperSetAdapter);
        paperSetAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            PaperSet paperSet = (PaperSet) adapter.getItem(position);
            onItemClick(paperSet);
        });

        registEmptyView(paperSetAdapter);
    }


    @Override
    public void initData() {
        type = (Type) getIntent().getSerializableExtra("type");
        mPresenter = new PaperContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getPaperSet(type.getId(), null, 2
                , false);
    }


    @Override
    public void setPaperSet(List<PaperSet> paperSets) {
        paperSetAdapter.resetData(paperSets);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setPaperSetInfo(PaperSet paperInfo) {

    }

    @Override
    public void setPaperList(ListWithPage<Paper> listWithPage) {

    }

    @Override
    public void setPaperSet(ListWithPage<PaperSet> paperSets) {

    }

    protected void onItemClick(PaperSet paperSet) {
        PaperCommonInfoActivity.start(mContext, paperSet.getId(), 2,-1);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
